package com.xzy.learn.spring.ioc.overview.dependency.inject;

import com.xzy.learn.spring.ioc.overview.config.DependencyInjectionDemoBeanConfig;
import com.xzy.learn.spring.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 依赖注入
 *
 * @author xzy.xiao
 * @date 2022/5/28  9:45
 */
public class DependencyInjectionDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new AnnotationConfigApplicationContext(DependencyInjectionDemoBeanConfig.class);

//        lookupByNameInRealTime(beanFactory);
        injectByNameInLazy(beanFactory);
    }

    /**
     * 实时注入
     *
     * @param beanFactory -
     */
    private static void injectByNameInRealTime(BeanFactory beanFactory) {
        UserRepository bean = beanFactory.getBean("userRepository1", UserRepository.class);
        System.out.println("实时注入：" + bean);
    }

    /**
     * 延迟注入
     *
     * @param beanFactory -
     */
    private static void injectByNameInLazy(BeanFactory beanFactory) {
        UserRepository bean = beanFactory.getBean("userRepository2", UserRepository.class);
        System.out.println("延时注入：" + bean);
        System.out.println("延时注入：" + bean.getUserObjectFactory().getObject());
    }
}
